package com.zone.module_api.test.viewmodel

import android.util.Log
import androidx.databinding.ObservableArrayList
import com.google.gson.reflect.TypeToken
import com.zone.api.database.ObjectBox
import com.zone.api.di.ApiInterceptor
import com.zone.api.model.ApiResponse
import com.zone.api.model.LoginResBean
import com.zone.api.model.UserInfoResBean
import com.zone.core.base.BaseViewModel
import com.zone.core.ext.assetsOpen
import com.zone.core.ext.fromJson
import com.zone.core.ext.toJson
import com.zone.module_api.test.model.ApiModel
import com.zone.module_api.test.repository.DataHttpRepository

class TestActivityVM(val repository: DataHttpRepository, objectBox: ObjectBox) : BaseViewModel() {
    val TAG = "TestActivityVM"
    val apis = ObservableArrayList<ApiModel>()

    init {
        apis.add(ApiModel("登录接口"))
        apis.add(ApiModel("个人信息接口"))
        apis.add(ApiModel("收藏接口"))
        apis.add(ApiModel("关注接口"))
        Log.d(TAG, "====================: " + apis.size)
    }

    fun request(api: ApiModel) {
        launch {
//            val loginResBean = LoginResBean("登录接口", "登录用户名username")
//            val userInfoResBean = UserInfoResBean("个人信息接口", "个人信息username")
//
//            ApiInterceptor.interceptorApi["/space-fourth/sys/phoneLogin"] =
//                ApiResponse(200, true, "登录成功", loginResBean)
//
//            ApiInterceptor.interceptorApi["/space-fourth/sys/userInfo"] =
//                ApiResponse(200, true, "个人信息接口", userInfoResBean)

            "apiData/_space-fourth_sys_phoneLogin.json".assetsOpen().let {
                val bean = it.fromJson<ApiResponse<LoginResBean>>(object :
                    TypeToken<ApiResponse<LoginResBean>>() {}.type)
                ApiInterceptor.interceptorApi["/space-fourth/sys/phoneLogin"] = bean
            }

            "apiData/_space-fourth_sys_userInfo.json".assetsOpen().let {
                val bean = it.fromJson<ApiResponse<UserInfoResBean>>(object :
                    TypeToken<ApiResponse<UserInfoResBean>>() {}.type)
                ApiInterceptor.interceptorApi["/space-fourth/sys/userInfo"] = bean
            }


            val rse = when (api.name) {
                "登录接口" -> {
                    repository.getLogin().toJson()
                }

                "个人信息接口" -> {
                    repository.getUserInfo().toJson()
                }

                else -> {
                    ""
                }
            }
            Log.d(TAG, "request: =======================$rse")
        }
    }


}